<?php
// +----------------------------------------------------------------------
// | HkCms 应用初始化
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace common\behavior;

class AppInitBehavior
{
    /**
     * app_init 行为执行
     * @param $param
     * @return mixed
     */
    public function run(&$param)
    {   
        // 注册AUTOLOAD方法
        spl_autoload_register('common\behavior\AppInitBehavior::autoload');
        $this->initialization();
    }

    /**
     * 站点初始化
     */
    private function initialization()
    {
        //产品版本号
        define("HKCMS_VERSION", config("version.hkcms_version"));

        //MODULE_ALLOW_LIST配置
        $moduleList = hk_cache('Module');
        $moduleAllowList = array('admin', 'api', 'attachment', 'content', 'install', 'template','addons');
        if(!empty($moduleList)){
            foreach ($moduleList as $rs) {
                if ($rs['disabled']) {
                    $moduleAllowList[] = $rs['module'];
                }
            }
        }
        config('MODULE_ALLOW_LIST', $moduleAllowList);
    }

    /**
     * 类库自动加载
     * @param string $class 对象类名
     * @return void
     */
    private function autoload($class) {
        //内容模型content_xx.class.php类自动加载
        if (in_array($class, array('content_form', 'content_input', 'content_output', 'content_update', 'content_delete'))) {
            \app\content\model\Content::classGenerate();
            require_cache(RUNTIME_PATH . "{$class}.php");
            return;
        }
    }
}